/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hjq.permissions.demo.slice;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.permissions.demo.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Component;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;

import java.util.List;

/**
 * 主界面
 * */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private ToastDialog toastDialog;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findComponentById(ResourceTable.Id_btn_main_request_1).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_main_request_2).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_main_request_3).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_main_request_4).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_main_request_5).setClickedListener(this);
        findComponentById(ResourceTable.Id_btn_main_app_details).setClickedListener(this);
    }

    private void toast(String message) {
        if (toastDialog != null) {
            toastDialog.cancel();
            toastDialog = null;
        }
        toastDialog = new ToastDialog(this);
        toastDialog.setText(message);
        toastDialog.setAlignment(TextAlignment.BOTTOM);
        toastDialog.show();
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_btn_main_request_1) {
            reqSingleCameraPermission();
        } else if (component.getId() == ResourceTable.Id_btn_main_request_2) {
            reqMorePermission();
        } else if (component.getId() == ResourceTable.Id_btn_main_request_3) {
            reqLocationPermission();
        } else if (component.getId() == ResourceTable.Id_btn_main_request_4) {
            reqStoragePermission();
        } else if (component.getId() == ResourceTable.Id_btn_main_request_5) {
            XXPermissions.with(this)
                    .permission("ohos.permission.GET_NETWORK_INFO")
                    .request(new OnPermission() {
                        @Override
                        public void hasPermission(List<String> granted, boolean all) {
                            if (all) {
                                toast("获取权限成功");
                            }
                        }

                        @Override
                        public void noPermission(List<String> denied, boolean never) {
                            toast("获取权限失败");
                        }
                    });
        } else if (component.getId() == ResourceTable.Id_btn_main_app_details) {
            XXPermissions.startPermissionActivity(this);
        } else {
            toast("null");
        }
    }

    private void reqStoragePermission() {
        XXPermissions.with(this)
                .permission(Permission.Group.USER_STORAGE)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        if (all) {
                            toast("获取存储权限成功");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予存储权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            String[] permissions = new String[denied.size()];
                            denied.toArray(permissions);
                            XXPermissions.startPermissionActivity(MainAbilitySlice.this,
                                    permissions, this);
                        } else {
                            toast("获取存储权限失败");
                        }
                    }
                });
    }

    private void reqLocationPermission() {
        XXPermissions.with(this)
                .permission(Permission.Group.LOCATION)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        if (all) {
                            toast("获取定位权限成功");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予定位权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            String[] permissions = new String[denied.size()];
                            denied.toArray(permissions);
                            XXPermissions.startPermissionActivity(MainAbilitySlice.this,
                                    permissions, this);
                            return;
                        }

                        if (denied.size() == 1 && Permission.LOCATION_IN_BACKGROUND.equals(denied.get(0))) {
                            toast("没有授予后台定位权限，请您选择\"始终允许\"");
                        } else {
                            toast("获取定位权限失败");
                        }
                    }
                });
    }

    private void reqMorePermission() {
        XXPermissions.with(this)
                .permission(Permission.RECORD_AUDIO)
                .permission(Permission.Group.CALENDAR)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        if (all) {
                            toast("获取录音和日历权限成功");
                        } else {
                            toast("获取部分权限成功，但是部分权限未正常授予");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予录音和日历权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            String[] permissions = new String[denied.size()];
                            denied.toArray(permissions);
                            XXPermissions.startPermissionActivity(MainAbilitySlice.this,
                                    permissions, this);
                        } else {
                            toast("获取权限失败");
                        }
                    }
                });
    }

    private void reqSingleCameraPermission() {
        XXPermissions.with(getAbility())
                .permission(Permission.CAMERA)
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean all) {
                        if (all) {
                            toast("获取拍照权限成功");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean never) {
                        if (never) {
                            toast("被永久拒绝授权，请手动授予拍照权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            String[] permissions = new String[denied.size()];
                            denied.toArray(permissions);
                            XXPermissions.startPermissionActivity(MainAbilitySlice.this,
                                    permissions, this);
                        } else {
                            toast("获取拍照权限失败");
                        }
                    }
                });
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        XXPermissions.onAbilityResult(requestCode);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
